package fr.znight.chacunsatv.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import fr.znight.chacunsatv.TVMainActivity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class ChatServer{
	
	boolean end=false;
	String messageFromPhone="";
	
	
	
	 public ChatServer(Context context){
	
	
	try {
        Boolean end = false;
        ServerSocket ss = new ServerSocket(12345);
        while(!end){
        	
            Log.d("waiting for the phone", "waiting for the phone");

                //Server is waiting for client here, if needed
                Socket s = ss.accept();
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
                String st = input.readLine();
                Log.d("Tcp Example", "From phone: "+st);
                output.println("Good bye and thanks for all the fish :)");
                s.close();
                
                messageFromPhone=st;
                
                if (messageFromPhone.contains("Hello"))
                	
                { 
                	
Log.d("","####Message du telephone="+messageFromPhone);

context.startActivity(new Intent(context, TVMainActivity.class));

                	end = true; 
                }
        }
ss.close();
       
       
} catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
	}
	
	
	
}